package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.EntrustOrder;
import com.gaogzhen.param.OrderParam;
import com.gaogzhen.service.EntrustOrderService;
import com.gaogzhen.utils.CurrentUser;
import com.gaogzhen.utils.R;
import com.gaogzhen.vo.TradeEntrustOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author Administrator
 * @date 2022-12-11 16:57
 */
@RestController
@RequestMapping("/entrustOrders")
@Api(tags = "委托记录的控制器")
public class EntrustOrderController {

    @Autowired
    private EntrustOrderService entrustOrderService;

    @GetMapping
    @ApiOperation(value = "查询用户的委托记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "symbol", value = "交易对"),
            @ApiImplicitParam(name = "type", value = "类型"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小")
    })
    public R<Page<EntrustOrder>> findByPage(@ApiIgnore Page<EntrustOrder> page, String symbol, Integer type) {
        return R.ok(entrustOrderService.findByPage(CurrentUser.getCurrentUserId(), page, symbol, type));
    }

    @GetMapping("/history/{symbol}")
    @ApiOperation(value = "查询历史的委托单记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "条数") ,
    })
    public R<Page<TradeEntrustOrderVo>> historyEntrustOrder(@ApiIgnore Page<EntrustOrder> page , @PathVariable("symbol") String symbol){
        Page<TradeEntrustOrderVo> pageData = entrustOrderService.getHistoryEntrustOrder(page,symbol,CurrentUser.getCurrentUserId()) ;
        return R.ok(pageData) ;
    }



    @GetMapping("/{symbol}")
    @ApiOperation(value = "查询未的委托单记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页") ,
            @ApiImplicitParam(name = "size",value = "条数") ,
    })
    public R<Page<TradeEntrustOrderVo>> entrustOrders(@ApiIgnore Page<EntrustOrder> page , @PathVariable("symbol") String symbol){
        Page<TradeEntrustOrderVo> pageData = entrustOrderService.getEntrustOrder(page,symbol,CurrentUser.getCurrentUserId()) ;
        return R.ok(pageData) ;
    }

    @PostMapping
    @ApiOperation(value = "委托单的下单操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderParam",value = "orderParam json数据")
    })
    public R createEntrustOrder(@RequestBody OrderParam orderParam){
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()) ;
        entrustOrderService.createEntrustOrder(userId,orderParam) ;
        return R.ok();
    }

    @ApiOperation(value = "委托单的取消操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "委托单的id")
    })
    @DeleteMapping("/{id}")
    public R deleteEntrustOrder(@PathVariable("id") Long orderId){
        entrustOrderService.cancleEntrustOrder(orderId) ;
        return R.ok("取消成功") ;
    }

}
